package com.uenpay.im.ui.old.activity.funds;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.base.BaseActivity;
import com.uenpay.im.ui.old.activity.pwd.SetPayPwdActivity;
import com.uenpay.im.ui.old.activity.wallet.ChargeActivity;
import com.uenpay.im.ui.old.activity.wallet.WalletHandle;
import com.uenpay.im.callback.IOnBottomDialogListener;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.DialogUtility;
import com.uenpay.im.weight.PayPwdDialog;
import com.uenpay.im.weight.RoundImageView;

import java.text.MessageFormat;
import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @E-mail:
 * @date 创建时间：2016-5-12 下午4:52:29
 * @version 1.0
 * @function 支付群经费详情列表
 */
public class FundsIncomeListActivity extends BaseActivity {

	private ListView listView;
	private double myBalance;
	private int haspaypassword;
	private String payAmount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_list_layout);
		ArrayList<FundsInDetailEntity> arrayList = (ArrayList<FundsInDetailEntity>) getIntent()
				.getSerializableExtra("arraylist");
		if (getIntent() != null && getIntent().hasExtra("pay_amount")) {
			payAmount = getIntent().getStringExtra("pay_amount");
		}
		initView();
		initListener();
		backListener();
		getMyBalance();
		FundsIncomeAdapter mFundsIncomeAdapter = new FundsIncomeAdapter(
				arrayList);
		listView.setAdapter(mFundsIncomeAdapter);
	}

	@Override
	protected void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.funds_detail);
		listView = (ListView) findViewById(R.id.listview);
		loading = findViewById(R.id.loading);
	}

	@Override
	protected void initListener() {
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				FundsInDetailEntity detailEntity = (FundsInDetailEntity) arg0
						.getAdapter().getItem(arg2);
				if (detailEntity != null
						&& detailEntity.status == 0
						&& detailEntity.custid.equals(IMApplication
								.getCustId())) {
					if (!TextUtils.isEmpty(payAmount)) {
						double temp = Double.valueOf(payAmount);
						if (temp > myBalance) {
							showInsufficientBalanceDialog();
						} else {
							if (haspaypassword == 1) {
								showPayPwdDialog(detailEntity.contributid + "");
							} else {
								showSetPwdDialog();
								// doContributToGroupFunds(
								// detailEntity.contributid + "", "");
							}
						}
					}

				}
			}
		});
	}

	/**
	 * 支付密码提示框
	 *
	 * @param contributid
	 */
	private void showPayPwdDialog(final String contributid) {
		PayPwdDialog.showBottomRedDialog(FundsIncomeListActivity.this,
				new PayPwdDialog.IPayPwdInputFinishListener() {

					@Override
					public void onPayPwdInputFinish(String payPwd) {
						doContributToGroupFunds(contributid, payPwd);
					}
				});
	}

	/**
	 * 余额不足提示
	 */
	private void showInsufficientBalanceDialog() {
		loading.setVisibility(View.GONE);
		DialogUtility.showDialog(FundsIncomeListActivity.this,
				R.string.insufficient_balance, R.string.sure, R.string.cancel,
				true, new IOnBottomDialogListener() {

					@Override
					public void onClicked() {
						Intent intent = new Intent(
								FundsIncomeListActivity.this,
								ChargeActivity.class);
						startActivityForResult(intent, 100);
					}
				});
	}

	/**
	 * 设置支付密码对话框
	 */
	private void showSetPwdDialog() {
		DialogUtility.showDialog(FundsIncomeListActivity.this,
				R.string.not_set_pay_pwd, R.string.sure, R.string.cancel, true,
				new IOnBottomDialogListener() {

					@Override
					public void onClicked() {
						Intent intent = new Intent(
								FundsIncomeListActivity.this,
								SetPayPwdActivity.class);
						startActivity(intent);
						finish();
					}
				});
	}

	private void doContributToGroupFunds(String contributid, String paypassword) {
		loading.setVisibility(View.VISIBLE);
		FundsHandle.doContributToGroupFunds(contributid, paypassword,
				new FundsHandle.IFundsListener() {

					@Override
					public void onFundsResult(int status, String msg) {
						loading.setVisibility(View.GONE);
						if (status == HttpInvokeResultCode.RESULT_OK) {
							setResult(RESULT_OK);
							finish();
						} else if (status == 2605) {
							// TODO
							showInsufficientBalanceDialog();
						}
					}
				});
	}

	private void getMyBalance() {
		WalletHandle.getBalance(new WalletHandle.IGetBalanceResultListener() {

			@Override
			public void onGetBalanceResult(String balance, int status,
										   int _haspaypassword) {
				if (status == HttpInvokeResultCode.RESULT_OK) {
					if (!TextUtils.isEmpty(balance)) {
						myBalance = Double.valueOf(balance);
					}
					haspaypassword = _haspaypassword;
				}
			}
		});
	}

	private class FundsIncomeAdapter extends BaseAdapter {

		ArrayList<FundsInDetailEntity> arrayList = null;

		private FundsIncomeAdapter(ArrayList<FundsInDetailEntity> _ArrayList) {
			arrayList = _ArrayList;
		}

		@Override
		public int getCount() {
			return arrayList == null ? 0 : arrayList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return arrayList == null ? null : arrayList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			FundsIncomeViewHodler fundsIncomeViewHodler = null;
			FundsInDetailEntity qyxUserEntity = arrayList.get(arg0);
			if (arg1 == null) {
				fundsIncomeViewHodler = new FundsIncomeViewHodler();
				arg1 = View.inflate(FundsIncomeListActivity.this,
						R.layout.item_funds_detail_layout, null);
				fundsIncomeViewHodler.avatar = (RoundImageView) arg1
						.findViewById(R.id.avatar_iv);
				fundsIncomeViewHodler.amountTv = (TextView) arg1
						.findViewById(R.id.amount_tv);
				fundsIncomeViewHodler.nickname = (TextView) arg1
						.findViewById(R.id.nick_name);
				fundsIncomeViewHodler.datetv = (TextView) arg1
						.findViewById(R.id.date_tv);
			}

			fundsIncomeViewHodler.avatar
					.SetUrl(APIConfiguration.getAvatarUrlBig(
							qyxUserEntity.custid, PacketType.PERSONAL));
			fundsIncomeViewHodler.nickname.setText(qyxUserEntity.nickname);
			if (qyxUserEntity.status == 1) {
				/** 已经缴费 **/
				fundsIncomeViewHodler.amountTv.setText(MessageFormat.format(
						getResources().getString(R.string.red_size),
						qyxUserEntity.amount));
				fundsIncomeViewHodler.amountTv.setTextColor(getResources()
						.getColor(R.color.the_color_black));
			} else {
				fundsIncomeViewHodler.amountTv.setText(R.string.not_pay);
				fundsIncomeViewHodler.amountTv.setTextColor(getResources()
						.getColor(R.color.the_main_color_normal));
			}
			fundsIncomeViewHodler.datetv.setText(qyxUserEntity.createtime);
			return arg1;
		}

		private class FundsIncomeViewHodler {
			private TextView amountTv, nickname, datetv;
			private RoundImageView avatar;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 100 && resultCode == RESULT_OK) {
			getMyBalance();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}
}
